<template>
	<!-- 搜索选择公用 
		必填项： url        请求url
				selected   父组件控制值
		<loadSelect  ref='selectop_supply'  :selected="selectedSupply"  url='/op/ent/get_ent_list?supOrBuyer=1' :isEdit='isEdit' v-on:valueChange='(val)=>supplyValueChange(val) '  v-on:request_list_finished='request_list_finished(2)'></loadSelect> 
		supplyValueChange(val){ 
	    	this.form.setFieldsValue({supplierCode:val})
	    	this.selectedSupply = val
	    },
	    //编辑状态需加此函数
	    request_list_finished(num){  
	    	//编辑状态下list请求完毕 
	    	this.spinning = false  
	    },		 
	-->
	<a-select 
		:value = 'selected' 
		placeholder="输入搜索..."
		:showSearch='true' 
		:filterOption="false"
		@search="handlesearch" 
		@select='handleselect'
		:notFoundContent="fetching ? undefined : '暂无数据，尝试输入关键字'"
		@popupScroll='popupScroll($event)'

	>
		<a-spin v-if="fetching" slot="notFoundContent" size="small"/>
		<a-select-option v-for="d in data_sup" :key="d.code"  v-else>{{d.name}}</a-select-option>
	</a-select>

</template>

<script >
import { loadSelectList }  from '@/api/common'
	export default {
		name:'loadSelect',
		props:{ 
			selected:{
				type:String,
				default:''
			}, 
			//请求data的url,可在url上添加请求参数
			url:{
				type:String,
				default:''
			},  
			//编辑状态，默认不是  
			isEdit:{
				type:Boolean,
				default:false,
			}
		},
		data(){
			return {
				 data_sup:[],
				 data_sup_total:null,
				 search:{
				 	pageSize:10,
				 	pageNo:1,
				 	keyWord:''
				 },
				 requestInit:false,
				 fetching:false,
			}
		},
		created(){ 
			if(this.isEdit == false){
				this.pageInit() 
			} 
		},
		methods:{
			pageInit(){
				loadSelectList(this.url,this.search).then((res)=>{
			 		if(res.data.flag == true){
			 			// resolve(res.data.dataList) 
			 			this.data_sup = res.data.dataList
			 			this.data_sup_total = res.data.totalRs 

			 			//编辑状态下
			 			if(this.isEdit == true){
			 				this.editRequest()
			 			} 
			 		}
			 	}).catch((err)=>{

			 	}) 
			},
			handlesearch(value) {
				// console.log(value)  
		    	/**值变化时设置  编辑请求列表值需验证value存在与否**/
		    	if(value && value != this.search.keyWord){
		    		this.requestInit = true
		    		this.search.pageNo = 1
		    		this.data_sup  =[]
		    	}
		    	/*设定搜索值*/
		        this.search.keyWord = value ? value:''
		        /*loading*/
		        this.fetching = this.requestInit == true? true : false;//因为这个占用v-if  导致数据重新渲染，所以会一直跳到scrollTop为0

		        loadSelectList(this.url,this.search)
		          .then(body => {
		          	this.data_sup_total = body.data.totalRs
		          	if(this.data_sup.length<this.data_sup_total){
				          	body.data.dataList.forEach((item)=>{
				          		this.data_sup.push({code:item.code,name:item.name})
				          	})

				          	//编辑状态下
				 			if(this.isEdit == true){
				 				this.editRequest()
				 			} 
		          	}
		          	else{
		          		if(this.isEdit == true){
		          			this.$emit('errorTip') 
			          	}
		          	}
		            this.fetching =  false;

		          });
			},
			editRequest(){
				let nextflag = this.data_sup.some((item)=>{ 
		 			return item.code == this.selected
		 		}) 
		 		if(nextflag == true){
		 			//请求完毕，loading结束 
		 			this.$emit('request_list_finished')
		 		}
		 		else{
		 			this.search.pageNo+=1
		 			this.handlesearch()
		 		}
			},
			popupScroll(e){
		      	// console.log(e.target.scrollTop,e.target.offsetHeight,e.target.scrollHeight)
		      	//数据全部请求完毕， 就不再调用这个函数
		      	if(this.data_sup_total == this.data_sup.length){
		      		return false
		      	}
		      	if(e.target.scrollTop + e.target.offsetHeight == e.target.scrollHeight){
		      		this.search.pageNo +=1
		    		this.requestInit = false
		    		this.handlesearch(this.search.keyWord) 
		      	}
		    },
		    handleselect(val){
		      	this.$emit('valueChange',val)
		    },
		},
		mounted(){ 
			this.$nextTick(()=>{
				//编辑时，等待selected值设定之后再请求列表
      			this.$on('edit',function(){
      				this.pageInit() 
      			})
      		})
		},
	}
</script>